package com.future;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 1807. 替换字符串中的括号内容
 * <p>
 * 输入：s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]
 * 输出："bobistwoyearsold"
 * 解释：
 * 键 "name" 对应的值为 "bob" ，所以将 "(name)" 替换为 "bob" 。
 * 键 "age" 对应的值为 "two" ，所以将 "(age)" 替换为 "two" 。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/evaluate-the-bracket-pairs-of-a-string
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 *
 * @author weiruibai.vendor
 * Date: 2023/1/12 09:32
 */
public class Solution_1807 {

    private static Solution_1807 instance = new Solution_1807();

    public static void main(String[] args) {
        /**
         *
         */
        String s = "(name)is(age)yearsold";
        List<List<String>> knowledge = new ArrayList<>();//[["name","bob"],["age","two"]]
        List<String> list1 = new ArrayList<String>() {{
            add("name");
            add("bob");
        }};
        List<String> list2 = new ArrayList<String>() {{
            add("age");
            add("two");
        }};
        knowledge.add(list1);
        knowledge.add(list2);
        /**
         * "hi(name)mybrother(key)"
         * [["a","b"]]
         */
        s = "hi(name)mybrother(key)";
        knowledge.clear();
        list1.clear();
        list1.add("a");
        list1.add("b");
        knowledge.add(list1);
        String evaluate = instance.evaluate(s, knowledge);
        evaluate = instance.evaluate_v2(s, knowledge);
        System.out.println(evaluate);
    }

    public String evaluate_v2(String s, List<List<String>> knowledge) {
        Map<String, String> map = new HashMap<>();
        for (List<String> list : knowledge) {
            map.put("(" + list.get(0) + ")", list.get(1));
        }
        StringBuilder result = new StringBuilder();
        int N = s.length();
        int start = 0;
        boolean add = false;
        for (int i = 0; i < N; i++) {
            char ch = s.charAt(i);
            if ('(' == ch) {
                add = true;
                start = i;
            } else if (')' == ch && add) {
                String key = s.substring(start, i + 1);
                String value = map.get(key);
                if (value == null) {
                    value = "?";
                }
                result.append(value);
                add = false;
            } else if (add) {

            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }

    /**
     * 超时
     *
     * @param s
     * @param knowledge
     * @return
     */
    public String evaluate(String s, List<List<String>> knowledge) {
        for (List<String> list : knowledge) {
            String key = "(" + list.get(0) + ")";
            if (!s.contains(key)) continue;
            String value = list.get(1);
            s = s.replace(key, value);
        }
        // 查看是否替换完
        while (s.contains("(")) {
            int N = s.length();
            int i = s.indexOf("(");
            for (int j = i; j < N; j++) {
                String replace = "";
                if (s.charAt(j) == ')') {
                    replace = s.substring(i, j + 1);
                    s = s.replace(replace, "?");
                    break;
                }
            }
        }
        return s;
    }

}
